﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Polls;

namespace NosoiFigthersWeb.PollDesigner {
	public partial class Elements : System.Web.UI.Page {
		protected void Page_Load(object sender, EventArgs e) {
			if (!IsPostBack) {
				Session["editedElement"] = null;
				Session["editedElementIndex"] = null;

				ViewState["elementsList"] = new List<string>();
				foreach (Text txt in (Session["poll"] as Poll).Elements)
					(ViewState["elementsList"] as List<string>).Add(txt.Content);

				BindElementsList();
			}
		}
		protected void Elements_ItemDataBound(object sender, ListViewItemEventArgs e) {
			if (e.Item is ListViewDataItem) {
				Label number = e.Item.FindControl("number") as Label;
				Label element = e.Item.FindControl("element") as Label;
				number.Text = "<b>" + ((e.Item as ListViewDataItem).DataItemIndex + 1).ToString() + ".</b> ";
				element.Text = (e.Item as ListViewDataItem).DataItem.ToString();
			}
		}
		private void UpdateElementsList() {
			List<string> elementsList = new List<string>();
			foreach (ListViewItem item in elements.Items)
				if (item is ListViewDataItem) {
					Label l = item.FindControl("element") as Label;
					elementsList.Add(l.Text);
				}
			ViewState["elementsList"] = elementsList;
		}
		private void BindElementsList() {
			List<string> elementsList = ViewState["elementsList"] as List<string>;

			elements.DataSource = elementsList;
			elements.DataBind();
		}
		protected void Down_Click(object sender, ImageClickEventArgs e) {
			int myIndex = ((sender as ImageButton).Parent as ListViewDataItem).DataItemIndex;
			int nextIndex = (myIndex + 1) % elements.Items.Count;

			var tmp1 = (Session["poll"] as Poll).Elements[myIndex];
			(Session["poll"] as Poll).Elements[myIndex] =
				(Session["poll"] as Poll).Elements[nextIndex];
			(Session["poll"] as Poll).Elements[nextIndex] = tmp1;

			ListViewDataItem tmp2 = elements.Items[myIndex];
			elements.Items[myIndex] = elements.Items[nextIndex];
			elements.Items[nextIndex] = tmp2;

			UpdateElementsList();
			BindElementsList();
		}
		protected void Up_Click(object sender, ImageClickEventArgs e) {
			int myIndex = ((sender as ImageButton).Parent as ListViewDataItem).DataItemIndex;
			int nextIndex = myIndex - 1;
			if (nextIndex < 0)
				nextIndex += elements.Items.Count;

			var tmp1 = (Session["poll"] as Poll).Elements[myIndex];
			(Session["poll"] as Poll).Elements[myIndex] =
				(Session["poll"] as Poll).Elements[nextIndex];
			(Session["poll"] as Poll).Elements[nextIndex] = tmp1;

			ListViewDataItem tmp2 = elements.Items[myIndex];
			elements.Items[myIndex] = elements.Items[nextIndex];
			elements.Items[nextIndex] = tmp2;

			UpdateElementsList();
			BindElementsList();
		}
		protected void Edit_Click(object sender, ImageClickEventArgs e) {
			int myIndex = ((sender as ImageButton).Parent as ListViewDataItem).DataItemIndex;
			Session["editedElement"] = (Session["poll"] as Poll).Elements[myIndex];
			Session["editedElementIndex"] = myIndex;

			Response.Redirect("ElementEdit.aspx");
		}
		protected void Delete_Click(object sender, ImageClickEventArgs e) {
			(Session["poll"] as Poll).Elements.RemoveAt(((sender as ImageButton).Parent
				as ListViewDataItem).DataItemIndex);
			elements.Items.Remove((sender as ImageButton).Parent as ListViewDataItem);

			UpdateElementsList();
			BindElementsList();
		}
		protected void AddNewElement_Click(object sender, EventArgs e) {
			Response.Redirect("ElementEdit.aspx");
		}
	}
}